package condition;

import java.util.Iterator;

import roborally.Position;
import elements.Element;
import elements.Robot;

/**
 * A class of unary basic conditions.
 * 
 * @version	3.0
 * @author 	Lies Tambeur 	r0264469	2Bcwsbbh
 * @author	Eline Vanrykel	r0264543	2Bcwselt
 *
 */
public class CanHitRobot extends UnaryBasicCondition {

	/**
	 * Returns true only if the element is able to shoot and it could 
	 * hit a robot if it did.
	 */
	@Override
	public boolean evaluate(Element element) {
		if(element instanceof Robot){
			boolean containsRobot = false;
			Robot robot = (Robot) element;
			if(robot.getBoard()!= null && robot.getBoard().getFirstElementStartingAt(robot.getPosition(), robot.getOrientation())!= null){
				Position position = robot.getBoard().getFirstElementStartingAt(robot.getPosition(), robot.getOrientation()).getPosition();
				Iterator<Element> it = robot.getBoard().getElements(position).iterator();
				while(it.hasNext()){
					Element itelement = it.next();
					if(itelement instanceof Robot){
						containsRobot = true;
					}
				}
			}
			return containsRobot && (!Robot.getEnergyRequiredToShoot().isSmallerThan(((Robot)robot).getEnergy()));
		}
		else{
			return false;
		}
		
	}
	
	@Override
	public String toString(){
		return "(can-hit-robot)";
	}
	
	@Override
	public boolean equals(Object object){
		if(object == null || this==null){
			return false;
		}
		return this.toString().equals(object.toString());
	}

}
